package com.lh.java.connect;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 ************************************************
 ** Module : Mysql 原始的预编译方式数据库联结
 ** Date: 2012-9-7  Time: 上午10:22:00 
 ** Author lh 
 ************************************************ 
 */
public class PreOriginalConnect {
	// 驱动程序名

	static String driver = "com.mysql.jdbc.Driver";

	// URL指向要访问的数据库名myad

	static String url = "jdbc:mysql://127.0.0.1:3306/myad";

	// MySQL配置时的用户名

	static String user = "root";

	// Java连接MySQL配置时的密码

	static String password = "root";

	public static void main(String[] args) {
		// OriginalConnect or =new OriginalConnect();
		try {
			// 加载驱动程序
			Class.forName(driver);
			// 连续数据库
			Connection conn = DriverManager.getConnection(url, user, password);
			if (!conn.isClosed()){
				System.out.println("Succeeded connecting to the Database!");
			}				
			// 要执行的SQL语句
			String sql = "select * from class_student_id where id = ?;DROP TABLE bb";
			// PreparedStatement用来执行SQL语句
			PreparedStatement statement = conn.prepareStatement(sql);
			statement.setInt(1, 1);
			ResultSet rs = statement.executeQuery();
			System.out.println("-----------------");
			System.out.println("执行结果如下所示:");
			System.out.println("-----------------");
			System.out.println(" 学号" + "\t" + " 姓名");
			System.out.println("-----------------");
			String name = null;
			while (rs.next()) {				
				System.out.println(rs.getString("student_id") + "\t" + rs.getString("name"));
			}
			rs.close();
			statement.close();
			conn.close();
			
		} catch (ClassNotFoundException e1) {
			System.out.println("Sorry,can`t find the Driver!");
			e1.printStackTrace();
		} catch (SQLException e2) {
			e2.printStackTrace();
		} catch (Exception e3) {
			e3.printStackTrace();
		}
		finally{
			
		}

	}

}
